package com.easy.sk.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.SeckillOrder;
import com.easy.common.service.ISeckillOrderService;
import com.easy.sk.system.mapper.SeckillOrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {


    @Override
    public SeckillOrder getSeckillOrderById(int id) {
        return super.getOne(new QueryWrapper<SeckillOrder>().eq("id", id));
    }

    @Override
    public SeckillOrder getSeckillOrderByUserId(int userId) {
        return super.getOne(new QueryWrapper<SeckillOrder>().eq("user_id", userId));
    }

    @Override
    public int addSeckillOrder(SeckillOrder seckillOrder) {
        // 确保删除状态默认为0(未删除)
        if (seckillOrder.getIs_del() == null) {
            seckillOrder.setIs_del(0);
        }
        return super.save(seckillOrder) ? 1 : 0;
    }

    @Override
    public SeckillOrder addSeckillOrder1(SeckillOrder seckillOrder) {
        // 确保删除状态默认为0(未删除)
        if (seckillOrder.getIs_del() == null) {
            seckillOrder.setIs_del(0);
        }
        return super.save(seckillOrder) ? seckillOrder : null;
    }

    @Override
    public int updateSeckillOrder(SeckillOrder seckillOrder) {
        return super.updateById(seckillOrder) ? 1 : 0;
    }

    @Override
    public int deleteSeckillOrder(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public List<SeckillOrder> getAllSeckillOrder(SeckillOrder seckillOrder) {
        LambdaQueryWrapper<SeckillOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillOrder::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<SeckillOrder> getSeckillOrderPage(SeckillOrder seckillOrder, IPage<SeckillOrder> page) {
        LambdaQueryWrapper<SeckillOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (seckillOrder.getId() != null){
            queryWrapper.eq(SeckillOrder::getId, seckillOrder.getId());
        }
        queryWrapper.like(seckillOrder.getActivity_id() != null, SeckillOrder::getActivity_id, seckillOrder.getActivity_id());
        queryWrapper.like(seckillOrder.getUser_id() != null, SeckillOrder::getUser_id, seckillOrder.getUser_id());
        queryWrapper.like(seckillOrder.getSeckill_goods_id() != null, SeckillOrder::getSeckill_goods_id, seckillOrder.getSeckill_goods_id());
        queryWrapper.like(seckillOrder.getSeckill_goods_name() != null, SeckillOrder::getSeckill_goods_name, seckillOrder.getSeckill_goods_name());
        queryWrapper.like(seckillOrder.getStatus() != null, SeckillOrder::getStatus, seckillOrder.getStatus());
        queryWrapper.eq(SeckillOrder::getIs_del, 0);
        return super.page(page, queryWrapper);
    }
}
